using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Easislides
{
	public class PopupWindowHelper : NativeWindow
	{
		private const int WM_ACTIVATE = 6;
		private const int WM_ACTIVATEAPP = 28;
		private const int WM_NCACTIVATE = 134;
		private const int KEYEVENTF_KEYUP = 2;
		private EventHandler popClosedHandler = null;
		private PopupWindowHelperMessageFilter filter = null;
		private Form popup = null;
		private Form owner = null;
		private bool popupShowing = false;
		private bool skipClose = false;
		public event PopupClosedEventHandler PopupClosed
		{
			[MethodImpl(MethodImplOptions.Synchronized)]
			add
			{
                //this.PopupClosed = (PopupClosedEventHandler)Delegate.Combine(this.PopupClosed, value);
			}
			[MethodImpl(MethodImplOptions.Synchronized)]
			remove
			{
                //this.PopupClosed = (PopupClosedEventHandler)Delegate.Remove(this.PopupClosed, value);
			}
		}
		public event PopupCancelEventHandler PopupCancel
		{
			[MethodImpl(MethodImplOptions.Synchronized)]
			add
			{
                //this.PopupCancel = (PopupCancelEventHandler)Delegate.Combine(this.PopupCancel, value);
			}
			[MethodImpl(MethodImplOptions.Synchronized)]
			remove
			{
                //this.PopupCancel = (PopupCancelEventHandler)Delegate.Remove(this.PopupCancel, value);
			}
		}
		[DllImport("user32", CharSet = CharSet.Auto)]
		private static extern int SendMessage(IntPtr handle, int msg, int wParam, IntPtr lParam);
		[DllImport("user32", CharSet = CharSet.Auto)]
		private static extern int PostMessage(IntPtr handle, int msg, int wParam, IntPtr lParam);
		[DllImport("user32")]
		private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
		public void ShowPopup(Form owner, Form popup, Point location)
		{
			this.owner = owner;
			this.popup = popup;
			Application.AddMessageFilter(this.filter);
			popup.StartPosition = FormStartPosition.Manual;
			popup.Location = location;
			owner.AddOwnedForm(popup);
			this.popClosedHandler = new EventHandler(this.popup_Closed);
			popup.Closed += this.popClosedHandler;
			this.popupShowing = true;
			popup.Show();
			popup.Activate();
			PopupWindowHelper.keybd_event(9, 0, 0, 0);
			PopupWindowHelper.keybd_event(9, 0, 2, 0);
			PopupWindowHelper.keybd_event(16, 0, 0, 0);
			PopupWindowHelper.keybd_event(9, 0, 0, 0);
			PopupWindowHelper.keybd_event(9, 0, 2, 0);
			PopupWindowHelper.keybd_event(16, 0, 2, 0);
			this.filter.Popup = popup;
		}
		private void popup_Closed(object sender, EventArgs e)
		{
			this.ClosePopup();
		}
		protected override void WndProc(ref Message m)
		{
			base.WndProc(ref m);
			if (this.popupShowing)
			{
				if (m.Msg == 134)
				{
					if ((int)m.WParam == 0)
					{
						PopupWindowHelper.SendMessage(base.Handle, 134, 1, IntPtr.Zero);
					}
				}
				else
				{
					if (m.Msg == 28)
					{
						if ((int)m.WParam == 0)
						{
							this.ClosePopup();
							PopupWindowHelper.PostMessage(base.Handle, 134, 0, IntPtr.Zero);
						}
					}
				}
			}
		}
		public void ClosePopup()
		{
			if (this.popupShowing)
			{
				if (!this.skipClose)
				{
					this.OnPopupClosed(new PopupClosedEventArgs(this.popup));
				}
				this.skipClose = false;
				this.owner.RemoveOwnedForm(this.popup);
				this.popupShowing = false;
				this.popup.Closed -= this.popClosedHandler;
				this.popClosedHandler = null;
				this.popup.Close();
				Application.RemoveMessageFilter(this.filter);
				this.owner.Activate();
				this.popup = null;
				this.owner = null;
			}
		}
		protected virtual void OnPopupClosed(PopupClosedEventArgs e)
		{
            //if (this.PopupClosed != null)
            //{
            //    this.PopupClosed(this, e);
            //}
		}
		private void popup_Cancel(object sender, PopupCancelEventArgs e)
		{
			this.OnPopupCancel(e);
		}
		protected virtual void OnPopupCancel(PopupCancelEventArgs e)
		{
            //if (this.PopupCancel != null)
            //{
            //    this.PopupCancel(this, e);
            //    if (!e.Cancel)
            //    {
            //        this.skipClose = true;
            //    }
            //}
		}
		public PopupWindowHelper()
		{
			this.filter = new PopupWindowHelperMessageFilter(this);
			this.filter.PopupCancel += new PopupCancelEventHandler(this.popup_Cancel);
		}
	}
}
